﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class CapVatTu : System.Web.UI.Page
{
    private static string ac = "user";
    clsXuLy ex = new clsXuLy();
    DataTable dt;
    SqlParameter[] p;
    string frmName = "frmCapVatTu";
    static int STT = 0;
    public string Header = System.Configuration.ConfigurationSettings.AppSettings["Header"].ToString();

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Title = "Đề nghị cấp VPP, thiết bị | " + Header;
            LoadDrop(dlChucVu, "sp_vnttblchucvu_List", "--Chọn chức vụ--");
            btnCancel_Click(sender, e);
        }
    }
    private void LoadDrop(DropDownList dl, string proc, string tieude)
    {
        dl.Items.Clear();
        dl.DataSource = ex.GetData(proc);
        dl.DataBind();
        dl.Items.Insert(0, new ListItem(tieude, ""));
        dl.SelectedIndex = 0;
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            Guid ID = Guid.NewGuid();
            p = new SqlParameter[6];
            p[0] = new SqlParameter("@IDs", ID);
            p[0].SqlDbType = SqlDbType.UniqueIdentifier;
            p[1] = new SqlParameter("@So", txtSo.Text.Trim());
            p[1].SqlDbType = SqlDbType.VarChar;
            p[2] = new SqlParameter("@HoTen", txtHoTen.Text.Trim());
            p[2].SqlDbType = SqlDbType.NVarChar;
            p[3] = new SqlParameter("@ChucVu", ex.GetGuid(dlChucVu.SelectedValue));
            p[3].SqlDbType = SqlDbType.UniqueIdentifier;
            p[4] = new SqlParameter("@BoPhan", txtBoPhan.Text.Trim());
            p[4].SqlDbType = SqlDbType.NVarChar;
            p[5] = new SqlParameter("@LyDo", txtLyDo.Text.Trim());
            p[5].SqlDbType = SqlDbType.NVarChar;

            ex.ExecuteNonQuerry("sp_vnttblcapvattu_Add", p);


            for (int i = 0; i < gv.Rows.Count; i++)
            {
                TextBox txtTen = (TextBox)gv.Rows[i].Cells[1].FindControl("txtTen");
                TextBox txtQuyCach = (TextBox)gv.Rows[i].Cells[2].FindControl("txtQuyCach");
                TextBox txtDVT = (TextBox)gv.Rows[i].Cells[3].FindControl("txtDVT");
                TextBox txtSLDN = (TextBox)gv.Rows[i].Cells[4].FindControl("txtSLDN");
                TextBox txtSLDuyet = (TextBox)gv.Rows[i].Cells[5].FindControl("txtSLDuyet");
                TextBox txtGhiChu = (TextBox)gv.Rows[i].Cells[6].FindControl("txtGhiChu");

                p = new SqlParameter[8];
                p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                p[1] = new SqlParameter("@IDCapVatTu", ID);
                p[1].SqlDbType = SqlDbType.UniqueIdentifier;
                p[2] = new SqlParameter("@TenVPP", txtTen.Text.Trim());
                p[2].SqlDbType = SqlDbType.NVarChar;
                p[3] = new SqlParameter("@DacTinh", txtQuyCach.Text.Trim());
                p[3].SqlDbType = SqlDbType.NVarChar;
                p[4] = new SqlParameter("@DonViTinh", txtDVT.Text.Trim());
                p[4].SqlDbType = SqlDbType.NVarChar;
                p[5] = new SqlParameter("@SLDeNghi", txtSLDN.Text.Trim().Equals("") ? 0 : int.Parse(txtSLDN.Text.Trim()));
                p[5].SqlDbType = SqlDbType.Int;
                p[6] = new SqlParameter("@SLDuyet", txtSLDuyet.Text.Trim().Equals("") ? 0 : int.Parse(txtSLDuyet.Text.Trim()));
                p[6].SqlDbType = SqlDbType.Int;
                p[7] = new SqlParameter("@GhiChu", txtGhiChu.Text.Trim());
                p[7].SqlDbType = SqlDbType.NVarChar;

                ex.ExecuteNonQuerry("sp_vnttblchitietcapvattu_Add", p);

                int s = ex.GetValueInt_Text("select count(*) from vnttblautonumber where upper(Name)='" + frmName.ToUpper() + "'");
                if (s.Equals(0))
                {
                    //Insert auto number
                    p = new SqlParameter[3];
                    p[0] = new SqlParameter("@IDs", Guid.NewGuid());
                    p[0].SqlDbType = SqlDbType.UniqueIdentifier;
                    p[1] = new SqlParameter("@Name", frmName);
                    p[1].SqlDbType = SqlDbType.VarChar;
                    p[2] = new SqlParameter("@Number", 1);
                    p[2].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_vnttblautonumber_Add", p);
                }
                else
                {
                    //Update auto number
                    p = new SqlParameter[2];
                    p[0] = new SqlParameter("@Name", frmName);
                    p[0].SqlDbType = SqlDbType.VarChar;
                    p[1] = new SqlParameter("@Number", txtSo.Text.Trim().Substring(6));
                    p[1].SqlDbType = SqlDbType.Int;
                    ex.ExecuteNonQuerry("sp_vnttblautonumber_Update", p);
                }
                gstGetMess("Lưu thành công", "");

            }
        }
        catch (Exception ax)
        {
            gstGetMess("Có lỗi trong quá trình lưu, xem trong file log", "");
            ex.WriteLog(Server.MapPath("Log.txt"), DateTime.Now.ToString() + "\tCấp vật tư\t" + ax.Message);
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtSo.Text = "";
        txtHoTen.Text = "";
        txtHoTen.Focus();
        dlChucVu.SelectedIndex = 0;
        txtBoPhan.Text = "";
        txtLyDo.Text = "";
        
        STT = ex.GetValueInt_Text("select count(*) from vnttblautonumber where upper(Name)='" + frmName.ToUpper() + "'");

        if (STT.Equals(0))
            STT++;
        else
            STT = ex.GetValueInt_Text("select isnull(Number,0) from vnttblautonumber where upper(name)='" + frmName.ToUpper() + "'") + 1;

        txtSo.Text = DateTime.Now.ToString("yyddMM") + STT.ToString();

        FirstGridViewRow();
    }
    protected void btnXem_Click(object sender, EventArgs e)
    {

    }
    protected void btnPDF_Click(object sender, EventArgs e)
    {

    }
    private void FirstGridViewRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("TenVPP", typeof(string)));
        dt.Columns.Add(new DataColumn("QuyCach", typeof(string)));
        dt.Columns.Add(new DataColumn("DVT", typeof(string)));
        dt.Columns.Add(new DataColumn("SLDN", typeof(string)));
        dt.Columns.Add(new DataColumn("SLDuyet", typeof(string)));
        dt.Columns.Add(new DataColumn("GhiChu", typeof(string)));


        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["TenVPP"] = string.Empty;
        dr["QuyCach"] = string.Empty;
        dr["DVT"] = string.Empty;
        dr["SLDN"] = string.Empty;
        dr["SLDuyet"] = string.Empty;
        dr["GhiChu"] = string.Empty;
        dt.Rows.Add(dr);
        
        ViewState["CurrentTable"] = dt;


        gv.DataSource = dt;
        gv.DataBind();

        EnableGV();

    }

    private void EnableGV()
    {
        for (int j = 0; j < gv.Rows.Count; j++)
        {
            TextBox txt1 = (TextBox)gv.Rows[j].Cells[4].FindControl("txtSLDN");
            TextBox txt2 = (TextBox)gv.Rows[j].Cells[5].FindControl("txtSLDuyet");
            if (ac.Equals("admin"))
            {
                txt1.Enabled = false;
                txt2.Enabled = true;
            }
            else
            {
                txt1.Enabled = true;
                txt2.Enabled = false;
            }
        }
    }
    private void AddNewRow()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtTen = (TextBox)gv.Rows[rowIndex].Cells[1].FindControl("txtTen");
                    TextBox txtQuyCach = (TextBox)gv.Rows[rowIndex].Cells[2].FindControl("txtQuyCach");
                    TextBox txtDVT = (TextBox)gv.Rows[rowIndex].Cells[3].FindControl("txtDVT");
                    TextBox txtSLDN = (TextBox)gv.Rows[rowIndex].Cells[4].FindControl("txtSLDN");
                    TextBox txtSLDuyet = (TextBox)gv.Rows[rowIndex].Cells[5].FindControl("txtSLDuyet");
                    TextBox txtGhiChu = (TextBox)gv.Rows[rowIndex].Cells[6].FindControl("txtGhiChu");
                    

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["TenVPP"] = txtTen.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["QuyCach"] = txtQuyCach.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DVT"] = txtDVT.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["SLDN"] = txtSLDN.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["SLDuyet"] = txtSLDuyet.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["GhiChu"] = txtGhiChu.Text.Trim();
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                gv.DataSource = dtCurrentTable;
                gv.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
        SetPreviousData();
        EnableGV();
    }
    private void SetRowData()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    TextBox txtTen = (TextBox)gv.Rows[rowIndex].Cells[1].FindControl("txtTen");
                    TextBox txtQuyCach = (TextBox)gv.Rows[rowIndex].Cells[2].FindControl("txtQuyCach");
                    TextBox txtDVT = (TextBox)gv.Rows[rowIndex].Cells[3].FindControl("txtDVT");
                    TextBox txtSLDN = (TextBox)gv.Rows[rowIndex].Cells[4].FindControl("txtSLDN");
                    TextBox txtSLDuyet = (TextBox)gv.Rows[rowIndex].Cells[5].FindControl("txtSLDuyet");
                    TextBox txtGhiChu = (TextBox)gv.Rows[rowIndex].Cells[6].FindControl("txtGhiChu");


                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;
                    dtCurrentTable.Rows[i - 1]["TenVPP"] = txtTen.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["QuyCach"] = txtQuyCach.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["DVT"] = txtDVT.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["SLDN"] = txtSLDN.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["SLDuyet"] = txtSLDuyet.Text.Trim();
                    dtCurrentTable.Rows[i - 1]["GhiChu"] = txtGhiChu.Text.Trim();
                    rowIndex++;
                }

                ViewState["CurrentTable"] = dtCurrentTable;
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }
    }
    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    TextBox txtTen = (TextBox)gv.Rows[rowIndex].Cells[1].FindControl("txtTen");
                    TextBox txtQuyCach = (TextBox)gv.Rows[rowIndex].Cells[2].FindControl("txtQuyCach");
                    TextBox txtDVT = (TextBox)gv.Rows[rowIndex].Cells[3].FindControl("txtDVT");
                    TextBox txtSLDN = (TextBox)gv.Rows[rowIndex].Cells[4].FindControl("txtSLDN");
                    TextBox txtSLDuyet = (TextBox)gv.Rows[rowIndex].Cells[5].FindControl("txtSLDuyet");
                    TextBox txtGhiChu = (TextBox)gv.Rows[rowIndex].Cells[6].FindControl("txtGhiChu");


                    gv.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                    txtTen.Focus();
                    txtTen.Text = dt.Rows[i]["TenVPP"].ToString();
                    txtQuyCach.Text = dt.Rows[i]["QuyCach"].ToString();
                    txtDVT.Text = dt.Rows[i]["DVT"].ToString();
                    txtSLDN.Text = dt.Rows[i]["SLDN"].ToString();
                    txtSLDuyet.Text = dt.Rows[i]["SLDuyet"].ToString();
                    txtGhiChu.Text = dt.Rows[i]["GhiChu"].ToString();

                    rowIndex++;
                }
            }
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        AddNewRow();
    }
    protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        SetRowData();
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            int rowIndex = Convert.ToInt32(e.RowIndex);
            if (dt.Rows.Count > 1)
            {
                dt.Rows.Remove(dt.Rows[rowIndex]);
                drCurrentRow = dt.NewRow();
                ViewState["CurrentTable"] = dt;
                gv.DataSource = dt;
                gv.DataBind();

                for (int i = 0; i < gv.Rows.Count - 1; i++)
                {
                    gv.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
                }
                SetPreviousData();
            }
        }
    }
    private void gstGetMess(string gstMess, string gstLink)
    {
        if (gstLink == "")
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "')", true);
        else
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), " ", "alert('" + gstMess + "');window.location.href='" + gstLink + "'", true);

    }
}